﻿@using Webdiyer.WebControls.Mvc
@using HiLand.General.Entity
@using HiLand.Utility4.MVC
@using GBFinance.Entity
@using GBFinance.Web.Models
@using HiLand.General
@using HiLand.Utility4.MVC.Data
@model PagedList<LoanBasicExtEntity>
@{
    ViewBag.Title = "Loan List";
    string queryName = this.GetPassedParam<PagedList<LoanBasicExtEntity>>("queryName");
    string queryValue = this.GetPassedParam<PagedList<LoanBasicExtEntity>>("queryValue");

    LoanTypes loanType = Miscs.GetCurrentLoanType();
    string loanListUrl = string.Empty;
    string sectionName = string.Empty;
    string sectionItemName = string.Empty;
    string pageTitle = string.Empty;

    if (loanType == LoanTypes.Secured)
    {
        loanListUrl = Url.Action("SecuredLoanList");
        sectionName = "aSecuredLoanSection";
        sectionItemName = "aSecuredLoanList";
        pageTitle = "Secured Loan List";
    }
    else
    {
        loanListUrl = Url.Action("UnSecuredLoanList");
        sectionName = "aUnSecuredLoanSection";
        sectionItemName = "aUnSecuredLoanList";
        pageTitle = "UnSecured Loan List";
    }
}
<style type="text/css">
    .modalFix
    {
        width: 650px;
    }
</style>
<h2>
    @pageTitle</h2>
<div id="ItemListDIV">
    @using (Html.BeginForm())
    {
        <select name="queryConditionName" id="queryConditionName">
            <option value="">Please select...</option>
            @if (queryName == "CustomerNo")
            {
                <option value="CustomerNo" selected="selected">Customer No.</option>
            }
            else
            { 
                <option value="CustomerNo">Customer No.</option>
            }

            @if (queryName == "BusinessName")
            {
                <option value="BusinessName" selected="selected">Business Name</option>
            }
            else
            { 
                <option value="BusinessName">Business Name</option>
            }

            @if (queryName == "MainApplicant")
            {
                <option value="MainApplicant" selected="selected">Main Applicant</option>
            }
            else
            { 
                <option value="MainApplicant">Main Applicant</option>
            }

            @if (queryName == "Email")
            {
                <option value="Email" selected="selected">Email</option>
            }
            else
            { 
                <option value="Email">Email</option>
            }

            @if (queryName == "Approved")
            {
                <option value="Approved" selected="selected">Approved</option>
            }
            else
            { 
                <option value="Approved">Approved</option>
            }
            @if (queryName == "Rejected")
            {
                <option value="Rejected" selected="selected">Rejected</option>
            }
            else
            { 
                <option value="Rejected">Rejected</option>
            }
            @if (queryName == "Deleted")
            {
                <option value="Deleted" selected="selected">Deleted</option>
            }
            else
            { 
                <option value="Deleted">Deleted</option>
            }
            @if (queryName == "Blacklisted")
            {
                <option value="Blacklisted" selected="selected">Blacklisted</option>
            }
            else
            { 
                <option value="Blacklisted">Blacklisted</option>
            }

            @if (queryName == "Collection")
            {
                <option value="Collection" selected="selected">Collection</option>
            }
            else
            { 
                <option value="Collection">Collection</option>
            }
        </select>
        <input name="queryConditionValue" id="queryConditionValue" value="@queryValue" />
        <input type="button" id="btnQuery" value="Query" />
    }
    <table width="98%" class="grid">
        <tr>
            <th style="width: 150px">
                Operate
            </th>
            <th style="width: 30px">
                Costom No.
            </th>
            <th style="width: 80px">
                 Enterprise Name
            </th>
            <th style="width: 80px">
                Loan Amount
            </th>
            <th style="width: 60px">
                Loan Date
            </th>
            <th style="width: 60px">
                Main Applicant
            </th>
            <th style="width: 60px">
                Email
            </th>
            <th>
               Loan Status
            </th>
            <th>
               Broker
            </th>
            <th style="width: 60px">
                Note
            </th>
            <th>
               FollowUp
            </th>
        </tr>
        @foreach (LoanBasicExtEntity entity in Model)
        {
            string enterpriseNote = "[EMPTY]";
            string enterpriseNoteTip = "EMPTY";
            if (string.IsNullOrWhiteSpace(entity.EnterpriseNote) == false)
            {
                enterpriseNoteTip = entity.EnterpriseNote;
                enterpriseNote = entity.EnterpriseNote;
                if (enterpriseNote.Length > 10)
                {
                    enterpriseNote = enterpriseNote.Substring(0, 10) + "...";
                }
            }

            string enterpriseFollowupHistory = "[EMPTY]";
            string enterpriseFollowupHistoryTip = "EMPTY";
            if (string.IsNullOrWhiteSpace(entity.EnterpriseFlowUpHistory) == false)
            {
                enterpriseFollowupHistoryTip = entity.EnterpriseFlowUpHistory;
                enterpriseFollowupHistory = entity.EnterpriseFlowUpHistory;
                if (enterpriseFollowupHistory.Length > 10)
                {
                    enterpriseFollowupHistory = enterpriseFollowupHistory.Substring(0, 10) + "...";
                }
            }
             
            <tr>
                <td>
                 @if (entity.LoanStatus == HiLand.General.LoanStatuses.Deleted)
                 { 
                        <a href="#" class="recoverLoan"  loanGuid="@entity.LoanGuid">Recover</a>
                 }
                 else
                 {
                    @Html.Link("Details", @Url.Action("LoanDetails", new { loanGuid = entity.LoanGuid }), null)
                    @Html.Link("Schedule", @Url.Action("ScheduleList", new { loanGuid = entity.LoanGuid }), null)
                    <a href="#" class="deleteLoan"  loanGuid="@entity.LoanGuid">Delete</a>
                 }
                </td>
                <td>@entity.EnterpriseIndexID
                </td>
                <td>@entity.LoanUserRealName
                </td>
                <td>@entity.LoanAmount.ToString()
                </td>
                <td>@entity.LoanDate.ToString(Miscs.DateTimeFormating)
                </td>
                <td>@entity.MainApplicant
                </td>
                <td>@entity.Email
                </td>
                <td>
                    <a href="#"  class="modalInput loanStatus" rel="#LoanStatusDisplayDIV" loanGuid="@entity.LoanGuid" >
                        @entity.LoanStatus.ToString()</a>
                </td>
                <td>
                    @entity.BrokerInfo
                </td>
                <td>
                    <a href="#"  class="modalInput enterpriseNote" rel="#EnterpriseNoteDisplayDIV" enterpriseID="@entity.LoanUserID" title="@enterpriseNoteTip">
                        @enterpriseNote</a>
                </td>
                <td>
                    <a href="#"  class="modalInput enterpriseFollowUpHistory" rel="#EnterpriseFollowUpHistoryDisplayDIV" enterpriseID="@entity.LoanUserID" title="@enterpriseFollowupHistoryTip">
                        @enterpriseFollowupHistory</a>
                </td>
            </tr>   
        }
    </table>
        <div class="modal modalFix" id="EnterpriseNoteDisplayDIV">
    </div>
    <div class="modal modalFix" id="EnterpriseFollowUpHistoryDisplayDIV">
    </div>
    <div class="modal modalFix" id="LoanStatusDisplayDIV">
    </div>
    @Html.Pager(Model, new PagerOptions() { PageIndexParameterName = "id", ShowDisabledPagerItems = false, AlwaysShowFirstLastPageNumber = true })
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#@sectionName").addClass("current");
            $("#@sectionItemName").addClass("current");
            
            $(".modalInput").overlay({
                mask: {
                    color: '#ebecff',
                    loadSpeed: 200,
                    opacity: 0.9
                },
                closeOnClick: false
            });

            $("a.enterpriseNote").click(function () {
                var targetID = $(this).attr("enterpriseID");
                var url = '@Url.Action("EnterpriseNote")' + '?enterpriseID=' + targetID+'&r='+ Math.random();
                $("#EnterpriseNoteDisplayDIV").load(url);
            });

            $("a.enterpriseFollowUpHistory").click(function () {
                var targetID = $(this).attr("enterpriseID");
                var url = '@Url.Action("EnterpriseFollowUpHistory")' + '?enterpriseID=' + targetID + '&r=' + Math.random();
                $("#EnterpriseFollowUpHistoryDisplayDIV").load(url);
            });

            $("a.loanStatus").click(function () {
                var targetID = $(this).attr("loanGuid");
                var url = '@Url.Action("LoanStatus")' + '?loanID=' + targetID;
                $("#LoanStatusDisplayDIV").load(url);
            });

            $("a.deleteLoan").click(function () {
                var result = confirm("need delete this record?");
                if (result == true) {
                    var targetID = $(this).attr("loanGuid");
                    var url = '@Url.Action("SetLoanStatusToDeleted")';
                    $.post(url, { loanGuid: targetID }, function (data) {
                        window.location.reload();
                    });
                }
            });

            $("a.recoverLoan").click(function () {
                var result = confirm("need recover this record?");
                if (result == true) {
                    var targetID = $(this).attr("loanGuid");
                    var url = '@Url.Action("SetLoanStatusToNew")';
                    $.post(url, { loanGuid: targetID }, function (data) {
                        window.location.reload();
                    });
                }
            });

            $("#btnQuery").click(function () {
                var queryName = $("#queryConditionName").val();
                var queryValue = $("#queryConditionValue").val();
                var url = '@loanListUrl' + '?queryConditionName=' + queryName + '&queryConditionValue=' + queryValue;
                window.location = url;
            });
        });
    </script>
</div>
